package com.mercury.ec.module.air.dal.mysql.demandorder;

import com.mercury.ec.framework.common.pojo.PageResult;
import com.mercury.ec.framework.mybatis.core.mapper.BaseMapperX;
import com.mercury.ec.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.mercury.ec.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandscheme.AirDemandSchemePageReqVO;
import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandscheme.AirDemandSchemeRespVO;
import com.mercury.ec.module.air.dal.dataobject.demandorder.AirDemandSchemeDO;
import com.mercury.ec.module.system.dal.dataobject.dept.DeptDO;
import com.mercury.ec.module.system.dal.dataobject.user.AdminUserDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 机票需求订单方案 Mapper
 *
 * @author hesx
 */
@Mapper
public interface AirDemandSchemeMapper extends BaseMapperX<AirDemandSchemeDO> {

    default PageResult<AirDemandSchemeDO> selectPage(AirDemandSchemePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AirDemandSchemeDO>()
                .eqIfPresent(AirDemandSchemeDO::getDemandId, reqVO.getDemandId())
                .eqIfPresent(AirDemandSchemeDO::getSerialNo, reqVO.getSerialNo())
                .eqIfPresent(AirDemandSchemeDO::getBookUserId, reqVO.getBookUserId())
                .eqIfPresent(AirDemandSchemeDO::getBookDeptId, reqVO.getBookDeptId())
                .betweenIfPresent(AirDemandSchemeDO::getBookTime, reqVO.getBookTime())
                .eqIfPresent(AirDemandSchemeDO::getFinallyQuoteUserId, reqVO.getFinallyQuoteUserId())
                .eqIfPresent(AirDemandSchemeDO::getFinallyQuoteDeptId, reqVO.getFinallyQuoteDeptId())
                .betweenIfPresent(AirDemandSchemeDO::getFinallyQuoteTime, reqVO.getFinallyQuoteTime())
                .eqIfPresent(AirDemandSchemeDO::getFinallyPricingUserId, reqVO.getFinallyPricingUserId())
                .eqIfPresent(AirDemandSchemeDO::getFinallyPricingDeptId, reqVO.getFinallyPricingDeptId())
                .betweenIfPresent(AirDemandSchemeDO::getFinallyPricingTime, reqVO.getFinallyPricingTime())
                .eqIfPresent(AirDemandSchemeDO::getCancelRuleId, reqVO.getCancelRuleId())
                .eqIfPresent(AirDemandSchemeDO::getCancelUserId, reqVO.getCancelUserId())
                .eqIfPresent(AirDemandSchemeDO::getCancelDeptId, reqVO.getCancelDeptId())
                .betweenIfPresent(AirDemandSchemeDO::getCancelTime, reqVO.getCancelTime())
                .eqIfPresent(AirDemandSchemeDO::getCancelCause, reqVO.getCancelCause())
                .likeIfPresent(AirDemandSchemeDO::getRemark, reqVO.getRemark())
                .eqIfPresent(AirDemandSchemeDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(AirDemandSchemeDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(AirDemandSchemeDO::getId)
        );
    }

    default List<AirDemandSchemeRespVO> selectByDemandId(Long demandId) {
        return selectJoinList(AirDemandSchemeRespVO.class, new MPJLambdaWrapperX<AirDemandSchemeDO>()
                .eq(AirDemandSchemeDO::getDemandId, demandId)
                .selectAll(AirDemandSchemeDO.class)
                .selectAs(AdminUserDO::getNickname, AirDemandSchemeRespVO::getBookUserNickname)
                .selectAs("u2.nickname", AirDemandSchemeRespVO::getFinallyQuoteUserNickname)
                .selectAs("u3.nickname", AirDemandSchemeRespVO::getFinallyPricingUserNickname)
                .selectAs("u4.nickname", AirDemandSchemeRespVO::getCancelUserNickname)
                .selectAs(DeptDO::getName, AirDemandSchemeRespVO::getBookDeptName)
                .selectAs("d2.name", AirDemandSchemeRespVO::getFinallyQuoteDeptName)
                .selectAs("d3.name", AirDemandSchemeRespVO::getFinallyPricingDeptName)
                .selectAs("d4.name", AirDemandSchemeRespVO::getCancelDeptName)
                .leftJoin(AdminUserDO.class, AdminUserDO::getId, AirDemandSchemeDO::getBookUserId)
                .leftJoin(AdminUserDO.class, "u2", AdminUserDO::getId, AirDemandSchemeDO::getFinallyQuoteUserId)
                .leftJoin(AdminUserDO.class, "u3", AdminUserDO::getId, AirDemandSchemeDO::getFinallyPricingUserId)
                .leftJoin(AdminUserDO.class, "u4", AdminUserDO::getId, AirDemandSchemeDO::getCancelUserId)
                .leftJoin(DeptDO.class, DeptDO::getId, AirDemandSchemeDO::getBookDeptId)
                .leftJoin(DeptDO.class, "d2", DeptDO::getId, AirDemandSchemeDO::getFinallyQuoteDeptId)
                .leftJoin(DeptDO.class, "d3", DeptDO::getId, AirDemandSchemeDO::getFinallyPricingDeptId)
                .leftJoin(DeptDO.class, "d4", DeptDO::getId, AirDemandSchemeDO::getCancelDeptId)
        );
    }

    default AirDemandSchemeDO selectByUnique(Long demandId, String serialNo) {
        return selectOne(AirDemandSchemeDO::getDemandId, demandId, AirDemandSchemeDO::getSerialNo, serialNo);
    }

    default int deleteByDemandId(Long demandId) {
        return delete(AirDemandSchemeDO::getDemandId, demandId);
    }
}
